


package com.devicemantra.coordinate.dataobjects;

import java.util.Iterator;
import java.util.List;

import javax.jdo.JDOException;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.JDOUserException;
import javax.jdo.PersistenceManager;

import com.devicemantra.coordinate.ds.tests.PMF;
import com.devicemantra.coordinate.exceptions.dataobjects.CDatabaseException;
import com.devicemantra.coordinate.exceptions.operations.CDepartmentDoesNotExistsException;
import com.devicemantra.coordinate.exceptions.operations.CDuplicateException;
import com.devicemantra.coordinate.exceptions.operations.CException;
import com.devicemantra.coordinate.exceptions.operations.CStateAlreadyExistsException;
import com.devicemantra.coordinate.exceptions.operations.CStateDoesNotExistsException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;


/** TODO - Most of the *Factory classes are written very badly 
 * put some thought into optimizing for performance */
public class CLocationFactory 
{
		public static boolean checkLocationExists(String pDepartmentName,
												 String pLocationId)
												 
		{
			boolean lDoesExist = true;
			/* First let us get the Department this location belongs to */
			CDepartment lDepartment = null;
			try {
				lDepartment = CDepartmentFactory.getDepartment(pDepartmentName);
			} catch (CDepartmentDoesNotExistsException e) {
				lDoesExist = false;
				return lDoesExist;
			}
			
			List<CLocation> lLocationList = lDepartment.getLocations();
			if( lLocationList != null ) {
				int lSizeOfList = lLocationList.size(); 
				for(int lCounter = 0; lCounter < lSizeOfList; lCounter++) {
					CLocation lLocation = lLocationList.get(lCounter);
					System.out.println("Location Id is " + lLocation.getLocationId());
				}
			}
			
			return lDoesExist;
		}
	
		public static void createNewLocation(Double pLatitude,
									  Double pLongitude,
									  String pDeptName,
									  String pStateName,
									  String pAddress,
									  String pLocationDesc) throws CException, CDepartmentDoesNotExistsException {
			
			/* First let us get the Department this location belongs to */
			CDepartment lDepartment = null;
			lDepartment = CDepartmentFactory.getDepartment(pDeptName);
			/* Create the new location */
			CLocation lNewLocation = new CLocation(	pLatitude,
													pLongitude,
													pDeptName,
													pStateName,
													pAddress,
													pLocationDesc);
			lDepartment.addLocation(lNewLocation);
			CDepartmentFactory.persistDepartment(lDepartment);
			
		}
		
		public static boolean checkLocationExists(String pLocationId) throws CDuplicateException 
		{
			boolean lIsTrue = false;
			String lLocationId = pLocationId;
			PersistenceManager pm = PMF.get().getPersistenceManager();
			pm.getFetchPlan().setMaxFetchDepth(3);
		    List<CLocation> lLocationList = null;
		    javax.jdo.Query query = pm.newQuery(CLocation.class);
			query.setFilter("mLocationId == lLocationId");
			query.declareParameters("String lLocationId");
		    lLocationList = (List<CLocation>) query.execute(lLocationId);
		    lLocationList = (List<CLocation>)pm.detachCopyAll(lLocationList);
		    
		    if(lLocationList.size() > 1 )
		    {
		    	throw new CDuplicateException("ERROR: More than one CLocation found with Id " + pLocationId);
		    }
		    
		   /* Check if size of list is 1 and lLocationList not null */
		   if( (lLocationList != null) && (lLocationList.size() == 1) ) {
			   lIsTrue = true;
			}
		   return lIsTrue;
		}
		
		public static String getOwningDepartment(String pLocationId) throws CDuplicateException 
		{
			boolean lIsTrue = false;
			String lLocationId = pLocationId;
			String lDepartmentName = null;
			PersistenceManager pm = PMF.get().getPersistenceManager();
			pm.getFetchPlan().setMaxFetchDepth(3);
		    List<CLocation> lLocationList = null;
		    javax.jdo.Query query = pm.newQuery(CLocation.class);
			query.setFilter("mLocationId == lLocationId");
			query.declareParameters("String lLocationId");
		    lLocationList = (List<CLocation>) query.execute(lLocationId);
		    lLocationList = (List<CLocation>)pm.detachCopyAll(lLocationList);
		    
		    if(lLocationList.size() > 1 )
		    {
		    	throw new CDuplicateException("ERROR: More than one CLocation found with Id " + pLocationId);
		    }
		    
		   /* Check if size of list is 1 and lLocationList not null */
		   if( (lLocationList != null) && (lLocationList.size() == 1) ) {
			   CLocation lLocation = lLocationList.get(0);
			   lDepartmentName = lLocation.getDepartmentName();
			}
		   return lDepartmentName;
		}
		
		/** TODO - Most of the *Factory classes are written very badly 
		 * put some thought into optimizing for performance 
		 * @throws CDuplicateException */
		 
		public static void modifyLocation(String pLocationId , 
											 CLocation pCLocation) throws CException, CDuplicateException {
			String lOwningDept = null; 
			CDepartment lDept = null;
			if(true == checkLocationExists(pLocationId)) {
				System.out.println("Owning department is " + getOwningDepartment(pLocationId));
				lOwningDept = getOwningDepartment(pLocationId);
				try {
					lDept = CDepartmentFactory.getDepartment(lOwningDept);
				} catch (CDepartmentDoesNotExistsException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					throw new CException(e.getMessage());
				}
			}
			
			List<CLocation> lLocationList = lDept.getLocations();
			int lListSize = lLocationList.size();
			for(int index=0; index <lListSize; index++ )
			{
				CLocation lLocation = lLocationList.get(index);
				String lLocationId = lLocation.getLocationId();
				if(lLocationId != null)
					if(lLocation.getLocationId().equals(pLocationId)) {
						lLocation.setLocationDescription(pCLocation.getLocationDescription());
						PersistenceManager pm = PMF.get().getPersistenceManager();
						try {
							pm.makePersistent(lLocation);
							}catch(JDOUserException e) {
							throw new CException(e.getMessage());
						}finally {
							pm.close();
						}
						break;
				}
			}
			
			
		}
		
		
		
		
}
